<?php

require_once("config.php");
require_once("utils.php");
require_once("Component.php");

class Lead extends Component {

	var $defaultColumns;
	var $columnMappings;
		
    function __construct($proxy){

		$this->defaultColumns = array(
				'lead_source',
				'campaign_id',
				'lead_source_description',
				'refered_by',
				'salutation',
				'first_name',
				'last_name',
				'account_name',
				'title',
				'department',
				'do_not_call',
				'assigned_user_id',
				'status',
				'status_description',
				'phone_work',
				'phone_mobile',
				'phone_home',
				'phone_other',
				'phone_fax',
				'email1',
				'primary_address_street',
				'primary_address_city',
				'primary_address_state',
				'primary_address_postalcode',
				'primary_address_country',
				'alt_address_street',
				'alt_address_city',
				'alt_address_state',
				'alt_address_postalcode',
				'alt_address_country',
				'description'
	        );

		$this->columnMappings = array ();

		parent::__construct("Leads", "leads", $proxy, $this->defaultColumns);
	
    }


    function getRecordCount() {

                $query = 'select count(*) row_count from leads ';
		$query .=  ' LEFT JOIN  email_addr_bean_rel on leads.id = email_addr_bean_rel.bean_id and email_addr_bean_rel.bean_module=\'Leads\' and email_addr_bean_rel.deleted=0 and email_addr_bean_rel.primary_address=1 ';
                $query .=  ' LEFT JOIN email_addresses on email_addresses.id = email_addr_bean_rel.email_address_id ' ;

		$account_name = isset($_REQUEST['account_name']) ? $_REQUEST['account_name'] : null;
                $customQuery = ($account_name != null) ? " leads.account_name=".$this->quote.$account_name.$this->quote : null;

                $ret = $this->getTotalRowCount($query, $customQuery, $this->columnMappings);
		return $ret;
    }

        
	function saveRecord($id, $lead_source, $campaign_id, $lead_source_description, $refered_by,
							$salutation, $first_name, $last_name, $account_name, $title,
							$department, $do_not_call, $assigned_user_id, $status, $status_description,
							$phone_work, $phone_mobile, $phone_home, $phone_other, $phone_fax, $email1, 
							$primary_address_street, $primary_address_city, $primary_address_state, 
							$primary_address_postalcode, $primary_address_country, $alt_address_street, 
							$alt_address_city, $alt_address_state, $alt_address_postalcode, $alt_address_country, 
							$description, $date_modified) {
							
			if (!$assigned_user_id)
				$assigned_user_id = $_SESSION["logged_in_user_id"];
		
			if ($id) {
							
				$call_arguments = array(
					'session' => $this->sess, 
    				'module' => 'Leads', 
    				'name_value_list' =>array(
									array("name" => "id", "value" => $id),
									array("name" => "lead_source", "value"  => $lead_source),
									array("name" => "campaign_id", "value"  => $campaign_id),
									array("name" => "lead_source_description", "value"  => $lead_source_description),
									array("name" => "refered_by", "value"  => $refered_by),
									array("name" => "salutation", "value"  => $salutation),
									array("name" => "first_name", "value"  => $first_name),
									array("name" => "last_name", "value"  => $last_name),
									array("name" => "account_name", "value"  => $account_name),
									array("name" => "title", "value"  => $title),
									array("name" => "department", "value"  => $department),
									array("name" => "do_not_call", "value"  => $do_not_call),
									array("name" => "assigned_user_id", "value"  => $assigned_user_id),
									array("name" => "status", "value"  => $status),
									array("name" => "status_description", "value"  => $status_description),
									array("name" => "phone_work", "value"  => $phone_work),
									array("name" => "phone_mobile", "value"  => $phone_mobile),
									array("name" => "phone_home", "value"  => $phone_home),
									array("name" => "phone_other", "value"  => $phone_other),
									array("name" => "phone_fax", "value"  => $phone_fax),
									array("name" => "email1", "value"  => $email1),
									array("name" => "primary_address_street", "value"  => $primary_address_street),
									array("name" => "primary_address_city", "value"  => $primary_address_city),
									array("name" => "primary_address_state", "value"  => $primary_address_state),
									array("name" => "primary_address_postalcode", "value"  => $primary_address_postalcode),
									array("name" => "primary_address_country", "value"  => $primary_address_country),
									array("name" => "alt_address_street", "value"  => $alt_address_street),
									array("name" => "alt_address_city", "value"  => $alt_address_city),
									array("name" => "alt_address_state", "value"  => $alt_address_state),
									array("name" => "alt_address_postalcode", "value"  => $alt_address_postalcode),
									array("name" => "alt_address_country", "value"  => $alt_address_country),
									array("name" => "description", "value"  => $description)
								));
				$response = $this->rest_request('set_entry', $call_arguments);
			} else {

				$call_arguments = array('session' => $this->sess, 
    				'module' => 'Leads', 
    				'name_value_list' =>array(
									array("name" => "lead_source", "value"  => $lead_source),
									array("name" => "campaign_id", "value"  => $campaign_id),
									array("name" => "lead_source_description", "value"  => $lead_source_description),
									array("name" => "refered_by", "value"  => $refered_by),
									array("name" => "salutation", "value"  => $salutation),
									array("name" => "first_name", "value"  => $first_name),
									array("name" => "last_name", "value"  => $last_name),
									array("name" => "account_name", "value"  => $account_name),
									array("name" => "title", "value"  => $title),
									array("name" => "department", "value"  => $department),
									array("name" => "do_not_call", "value"  => $do_not_call),
									array("name" => "assigned_user_id", "value"  => $assigned_user_id),
									array("name" => "status", "value"  => $status),
									array("name" => "status_description", "value"  => $status_description),
									array("name" => "phone_work", "value"  => $phone_work),
									array("name" => "phone_mobile", "value"  => $phone_mobile),
									array("name" => "phone_home", "value"  => $phone_home),
									array("name" => "phone_other", "value"  => $phone_other),
									array("name" => "phone_fax", "value"  => $phone_fax),
									array("name" => "email1", "value"  => $email1),
									array("name" => "primary_address_street", "value"  => $primary_address_street),
									array("name" => "primary_address_city", "value"  => $primary_address_city),
									array("name" => "primary_address_state", "value"  => $primary_address_state),
									array("name" => "primary_address_postalcode", "value"  => $primary_address_postalcode),
									array("name" => "primary_address_country", "value"  => $primary_address_country),
									array("name" => "alt_address_street", "value"  => $alt_address_street),
									array("name" => "alt_address_city", "value"  => $alt_address_city),
									array("name" => "alt_address_state", "value"  => $alt_address_state),
									array("name" => "alt_address_postalcode", "value"  => $alt_address_postalcode),
									array("name" => "alt_address_country", "value"  => $alt_address_country),
									array("name" => "description", "value"  => $description)
					)
				);
				$response = $this->rest_request('set_entry', $call_arguments);		
			}
			return $response;
	}
}
	
$component = new Lead($sugarcrm_rest_url);

					
$leadsArray = array("" => "",
				    "Cold Call" => "Cold Call",
					"Existing Customer" => "Existing Customer",
					"Self Generated" => "Self Generated",
					"Employee" => "Employee",
					"Partner" => "Partner",
					"Public Relations" => "Public Relations",
					"Direct Mail" => "Direct Mail",
					"Conference" => "Conference",
					"Trade Show" => "Trade Show",
					"Web Site" => "Web Site",
					"Word of mouth" => "Word of mouth",
					"Email" => "Email",
					"Campaign" => "Campaign",
					"Other" => "Other");
					
$statusArray = array ("" => "",
					"New" => "New",
					"Assigned" => "Assigned",
					"In Process" => "In Process",
					"Converted" => "Converted",
					"Recycled" => "Recycled",
					"Dead" => "Dead");
					
$salutationsArray = array("" => "", 
 						  "Mr." => "Mr.", 
						  "Ms." => "Ms.", 
						  "Mrs." => "Mrs.", 
						  "Dr." => "Dr.", 
						  "Prof." => "Prof.");

?>
